package com.zkt.subject.domain.service.impl;

import cn.hutool.core.collection.ListUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zkt.subject.common.constants.SubjectConstant;
import com.zkt.subject.common.entity.PageResult;
import com.zkt.subject.domain.convert.SubjectUserRecordConverter;
import com.zkt.subject.domain.entity.SubjectUserRecordBO;
import com.zkt.subject.domain.service.SubjectInfoDomainService;
import com.zkt.subject.domain.service.SubjectUserRecordDomainService;
import com.zkt.subject.infra.basic.entity.SubjectLabel;
import com.zkt.subject.infra.basic.entity.SubjectMapping;
import com.zkt.subject.infra.basic.entity.SubjectUserRecord;
import com.zkt.subject.infra.basic.service.ISubjectLabelService;
import com.zkt.subject.infra.basic.service.ISubjectMappingService;
import com.zkt.subject.infra.basic.service.ISubjectUserRecordService;
import com.zkt.subject.infra.entity.SubjectInfoBO;
import jakarta.annotation.Resource;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;

/**
 * @author 赵开泰
 * @program jc-club
 * @date 2025/3/29
 * @description 用户做题记录service层实现类
 **/

@Service
public class SubjectUserRecordDomainServiceImpl implements SubjectUserRecordDomainService {
	
	@Resource
	private ISubjectUserRecordService subjectUserRecordService;
	
	@Resource
	private ISubjectMappingService subjectMappingService;
	
	@Resource
	private ISubjectLabelService subjectLabelService;
	
	@Resource
	private SubjectInfoDomainService subjectInfoDomainService;
	
	@Resource
	private RocketMQTemplate rocketMQTemplate;
	
	/**
	 * 增加用户做题记录
	 *
	 * @param subjectUserRecordBO 用户做题记录
	 * @return 是否成功
	 */
	@Override
	public Boolean add(SubjectUserRecordBO subjectUserRecordBO) {
		SubjectUserRecord subjectUserRecord = SubjectUserRecordConverter.INSTANCE.convertBoToUserRecord(subjectUserRecordBO);
		
		rocketMQTemplate.convertAndSend(SubjectConstant.DEAL_NEW_QUESTION, subjectUserRecord);
		return subjectUserRecordService.save(subjectUserRecord);
	}
	
	/**
	 * 获取用户做题记录
	 *
	 * @param subjectUserRecordBO 查询参数
	 * @return 是否成功
	 */
	@Override
	public PageResult<SubjectUserRecordBO> getSubjectUserRecord(SubjectUserRecordBO subjectUserRecordBO) {
		PageResult<SubjectUserRecordBO> pageResult = new PageResult<>();
		pageResult.setPageNo(subjectUserRecordBO.getPageNo());
		pageResult.setPageSize(subjectUserRecordBO.getPageSize());
		int start = (subjectUserRecordBO.getPageNo() - 1) * subjectUserRecordBO.getPageSize();
		
		SubjectUserRecord subjectUserRecord = SubjectUserRecordConverter.INSTANCE.convertBoToUserRecord(subjectUserRecordBO);
		
		Long total = subjectUserRecordService.count(subjectUserRecord);
		List<SubjectUserRecordBO> subjectUserRecordBOList = ListUtil.empty();
		if (total > 0) {
			List<SubjectUserRecord> subjectUserRecordPage = subjectUserRecordService.queryPage(subjectUserRecord, start,
					subjectUserRecordBO.getPageSize());
			
			subjectUserRecordBOList = SubjectUserRecordConverter.INSTANCE.convertUserRecordListToBo(subjectUserRecordPage);
		}
		subjectUserRecordBOList.forEach(s -> {
			List<Long> labelIds = subjectMappingService.lambdaQuery()
					.eq(SubjectMapping::getSubjectId, s.getSubjectId())
					.list()
					.stream()
					.map(SubjectMapping::getLabelId)
					.toList();
			List<String> labelNames = subjectLabelService.listByIds(labelIds).stream()
					.map(SubjectLabel::getLabelName)
					.toList();
			s.setLabelNames(labelNames);
			
			SubjectInfoBO subjectInfoBO = new SubjectInfoBO();
			subjectInfoBO.setId(s.getSubjectId());
			SubjectInfoBO subjectInfo = subjectInfoDomainService.querySubjectInfo(subjectInfoBO);
			
			s.setSubjectAnswer(subjectInfo.getSubjectAnswer());
			s.setOptionList(subjectInfo.getOptionList());
		});
		
		pageResult.setResult(subjectUserRecordBOList);
		pageResult.setTotal(total);
		return pageResult;
	}
	
	/**
	 * 获取用户所有的做题记录
	 *
	 * @param subjectUserRecordBO 查询参数
	 * @return 查询结果
	 */
	@Override
	public List<SubjectUserRecordBO> getAllSubjectUserRecord(SubjectUserRecordBO subjectUserRecordBO) {
		SubjectUserRecord subjectUserRecord = SubjectUserRecordConverter.INSTANCE.convertBoToUserRecord(subjectUserRecordBO);
		
		List<SubjectUserRecord> subjectUserRecordList = subjectUserRecordService.query(subjectUserRecord);
		
		return SubjectUserRecordConverter.INSTANCE.convertUserRecordListToBo(subjectUserRecordList);
	}
}